package com.peanut.baby.mvp.bbs.publish;

import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.peanut.baby.InitManager;
import com.peanut.baby.http.BaseObserver;
import com.peanut.baby.http.BaseResponse;
import com.peanut.baby.http.BaseScheduler;
import com.peanut.baby.http.RetrofitClient;
import com.peanut.baby.model.BBSMoment;
import com.peanut.baby.model.UploadConf;
import com.peanut.baby.uploader.UploadMgr;
import com.peanut.devlibrary.compressor.Compressor;
import com.yanzhenjie.alertdialog.AlertDialog;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionListener;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;

import java.io.File;
import java.util.List;

import cn.nekocode.rxlifecycle.RxLifecycle;
import io.reactivex.Observable;

/**
 * Created by anonymouslystudio on 2017/7/28.
 */

public class BBSPublishPresenter implements BBSPublishContract.Presenter{
    private BBSPublishContract.View view;
    private Activity                activity;
    public BBSPublishPresenter(BBSPublishContract.View view,Activity activity){
        this.view = view;
        this.activity = activity;
    }


    @Override
    public void checkPicturePermission(final Activity activity) {
        AndPermission.with(activity)
                .requestCode(100)
                .permission(Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .rationale(new RationaleListener(){

                    @Override
                    public void showRequestPermissionRationale(int requestCode, final Rationale rationale) {
                        AlertDialog.newBuilder(activity)
                                .setTitle("提醒")
                                .setMessage("你已拒绝过相机或写文件权限，再次拒绝将无法上传照片哦！")
                                .setPositiveButton("授予", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        rationale.resume();
                                    }
                                })
                                .setNegativeButton("拒绝", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        rationale.cancel();
                                    }
                                })
                                .show();
                    }
                })
                .callback(new PermissionListener() {
                    @Override
                    public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
                        view.onPicturePermissionGranted();
                    }

                    @Override
                    public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
                        view.onPicturePermissionDenied();
                    }
                })
                .start();
    }

    @Override
    public void compressAndUploadPicture(final String path) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                File compressed = Compressor.compress(new File(path));
                UploadMgr.getInstance().uploadImage(compressed.getAbsolutePath(),
                        UploadMgr.UPLOAD_TYPE_MOMENT, InitManager.getInstance().getUserId(), new UploadMgr.UploadListener() {
                            @Override
                            public void onQueryToken() {
                            }

                            @Override
                            public void onStartUpload() {

                            }

                            @Override
                            public void onUploadProgress(int total) {

                            }

                            @Override
                            public void onUploadSuccess(UploadConf config) {
                                view.onPictureUploadFinished(true,"",config);
                            }

                            @Override
                            public void onUploadFailed(UploadConf config,String failed) {
                                view.onPictureUploadFinished(false,failed,config);
                            }
                        });
            }
        }).start();
    }

    @Override
    public void submitMoment(String title,String content,String type,String pictures) {
        Observable observable = RetrofitClient.getInstance().submitMoment(InitManager.getInstance().getUserId(),
                type,
                title,
                content,
                pictures,
                InitManager.getInstance().getUser().userToken);
        observable.compose(BaseScheduler.<BaseResponse<BBSMoment>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<BBSMoment>() {
            @Override
            protected void onHandleSuccess(BBSMoment o) {
                view.onSendMomentSuccess(o);
            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onSendMomentFailed(msg);
            }

        });
    }

}
